package com.example.demo.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.sys.entity.Coursevideo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.demo.controller.Result;
import com.example.demo.sys.mapper.WorkMapper;
import com.example.demo.sys.entity.Work;

import org.springframework.web.bind.annotation.RestController;
import com.example.demo.common.tools.BaseController;

/**
 * <p>
    *  前端控制器 chrimer
    * </p>
 *
 * @author chrimer
 * @since 2020-07-01
 */
@RestController
@RequestMapping("/sys/work")
public class WorkController extends BaseController {
    @Autowired
    private WorkMapper workMapper;

    @RequestMapping(method= RequestMethod.POST,value="/page")
    public Result page(
        Integer pageNum,
        Integer pageSize) {
        Result result = new Result();
        Page<Work> page = new Page<Work>(pageNum, pageSize);
    Work parms = new Work();
        QueryWrapper<Work> warpper = new QueryWrapper<Work>(parms);

        result.setData(workMapper.selectPage(page, warpper));
        return result;
    }

    //添加作业
    @RequestMapping(method= RequestMethod.POST,value="/save")
    public Result save(@RequestBody Work entity) {
        Result result = new Result();
        Work test = new Work();
        test.setId(entity.getId());
        test.setCourseId(entity.getCourseId());
        Work workExit = workMapper.selectOne(new QueryWrapper<Work>(test));
        if (workExit == null) {
            String parantName = workMapper.SelectParentName(entity.getId());
            entity.setParentName(parantName);
            workMapper.insert(entity);
            result.success(entity.getParentName()+"作业成功发布");
        } else {
            String parantName = workMapper.SelectParentName(entity.getId());
            entity.setParentName(parantName);
            workMapper.updateById(entity);
            result.success(entity.getParentName()+"作业成功更新");
        }

        return result;
    }

    //查看作业
    @RequestMapping(method= RequestMethod.POST,value="/select")
    public Result select(String id) {
        Result result = new Result();
        Work parms = new Work();
        QueryWrapper< Work> warpper = new QueryWrapper< Work>(parms);
        List<Work>  workInfor = null;
        warpper.eq("courseId",id);
        warpper.orderByAsc("parentName");
        workInfor = workMapper.selectList(warpper);
        Map<String, Object> map = new HashMap<>();
        map.put("workInfor",  workInfor);
        result.setData(map);
        return result;
    }

    @RequestMapping(method= RequestMethod.DELETE,value="/delete")
    public Result delete( String ids) {
        Result result = new Result();
        List<String> deleteIds = new ArrayList<String>();
        for (String id : ids.split(",")) {
            deleteIds.add(id);
        }
    workMapper.deleteBatchIds(deleteIds);
        return result;
    }

}
